﻿using System;
using System.Collections.Generic;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreGraphics;
using MonoTouch.ObjCRuntime;
using System.Drawing;
namespace Beauty
{
	public class productCollectionViewController : UICollectionViewController
	{
	 	static NSString productCellId = new NSString("serviceCell");  
		static NSString headerId = new NSString ("Header");
		static NSString footerId = new NSString ("Footer");
		static int Type =0;
		List<ServiceItem> listProducts;


		public productCollectionViewController (UICollectionViewLayout layout,int type) : base (layout)
		{
			Type = type;
			listProducts = new List<ServiceItem> ();
			for (int i = 0; i < 12; i++) {
				listProducts.Add (new ServiceItem ());
			}
		}

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			if (Type == 0) {
				CollectionView.RegisterClassForCell (typeof(serviceCell), productCellId);
			} else {
				CollectionView.RegisterClassForCell (typeof(productCellList), productCellId);
			}
			CollectionView.BackgroundColor = UIColorExtensions.FromHexString ("#ffffff");
 			CollectionView.RegisterClassForSupplementaryView (typeof(Header), UICollectionElementKindSection.Header, headerId);
			CollectionView.RegisterClassForSupplementaryView (typeof(Footer), UICollectionElementKindSection.Footer, footerId);
		//	CollectionView.Frame = new RectangleF (0, 20, 615, 650);
			CollectionView.Layer.BorderWidth = 1f;
			CollectionView.Layer.BorderColor = UIColorExtensions.FromHexString("#b2b2b2").CGColor;
			// add a custom menu item
//			UIMenuController.SharedMenuController.MenuItems = new UIMenuItem[] { 
//				new UIMenuItem ("Custom", new Selector ("custom:")) 
//			};
		}

		public override int NumberOfSections (UICollectionView collectionView)
		{
			return 1;
		}

		public override int GetItemsCount (UICollectionView collectionView, int section)
		{
			return listProducts.Count;
		}

		public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
		{

//			if (Type == 0) {
			var  productCell = (serviceCell)collectionView.DequeueReusableCell (productCellId, indexPath);
//			} else {
			//	var  productCell = (productCellList)collectionView.DequeueReusableCell (productCellId, indexPath);
			//}
			var productItem = listProducts [indexPath.Row];

			productCell.Image = productItem.ServiceImage;

			return productCell;
		}
		 
		public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
 		{
			if (elementKind == "UICollectionElementKindSectionHeader") {
	 			var headerView = (Header)collectionView.DequeueReusableSupplementaryView (elementKind, headerId, indexPath);
	 			return headerView;
 			}
 		   
			if (elementKind == "UICollectionElementKindSectionFooter") {
 				var footerView = (Footer)collectionView.DequeueReusableSupplementaryView (elementKind, footerId, indexPath);
 	 			return footerView;
 		  }
 		 
		 	return new UICollectionReusableView ();
		}

		public override void ItemHighlighted (UICollectionView collectionView, NSIndexPath indexPath)
		{
			var cell = collectionView.CellForItem (indexPath);
			cell.ContentView.BackgroundColor = UIColor.Yellow;
		}

		public override void ItemUnhighlighted (UICollectionView collectionView, NSIndexPath indexPath)
		{
			var cell = collectionView.CellForItem (indexPath);
			cell.ContentView.BackgroundColor = UIColor.White;
		}

		public override bool ShouldHighlightItem (UICollectionView collectionView, NSIndexPath indexPath)
		{
			return true;
		}

		//      public override bool ShouldSelectItem (UICollectionView collectionView, NSIndexPath indexPath)
		//      {
		//          return false;
		//      }

		// for edit menu
		public override bool ShouldShowMenu (UICollectionView collectionView, NSIndexPath indexPath)
		{
			return true;
		}

		public override bool CanPerformAction (UICollectionView collectionView, MonoTouch.ObjCRuntime.Selector action, NSIndexPath indexPath, NSObject sender)
		{
			return true;
		}

		public override void PerformAction (UICollectionView collectionView, MonoTouch.ObjCRuntime.Selector action, NSIndexPath indexPath, NSObject sender)
		{
			Console.WriteLine ("code to perform action");
		}

		// CanBecomeFirstResponder and CanPerform are needed for a custom menu item to appear
		public override bool CanBecomeFirstResponder {
			get {
				return true;
			}
		}

		public override bool CanPerform (Selector action, NSObject withSender)
		{
			if (action == new Selector ("custom"))
				return true;
			else
				return false;
		}

		// System provided cut, copy and paste will be sent to PerformAction method above, but any custom menu items
		// must have their assocatied actions implementated explicitly
		[Export("custom")]
		void Custom()
		{
			Console.WriteLine ("custom");
		}

 		public override void WillRotate (UIInterfaceOrientation toInterfaceOrientation, double duration)
 		{
 			base.WillRotate (toInterfaceOrientation, duration);
 
// 			var lineLayout = CollectionView.CollectionViewLayout as LineLayout;
// 			if (lineLayout != null)
// 			{
// 				if((toInterfaceOrientation == UIInterfaceOrientation.Portrait) || (toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown))
// 					lineLayout.SectionInset = new UIEdgeInsets (400,0,400,0);
// 				else
// 					lineLayout.SectionInset  = new UIEdgeInsets (220, 0.0f, 200, 0.0f);
// 			}
 		}

	}

	 

	public class Header : UICollectionReusableView
	{
		 
		 

		[Export ("initWithFrame:")]
		public Header (System.Drawing.RectangleF frame) : base (frame)
		{
			UISearchBar temp = new UISearchBar( new RectangleF(20, 15, 575, 40));
		 	temp.Layer.BorderWidth = 0.5f;
			temp.Layer.CornerRadius = 20f;
		 	//temp.BackgroundColor = UIColor.White ;
			 temp.BarTintColor = UIColor.White;
	 		AddSubview (temp);
		}
	}
	public class Footer : UICollectionReusableView
	{

		[Export ("initWithFrame:")]
		public Footer (System.Drawing.RectangleF frame) : base (frame)
		{
			UIPageControl temp = new UIPageControl( new RectangleF(80, 15, 450, 30));
			temp.CurrentPageIndicatorTintColor = UIColor.LightGray;
			temp.PageIndicatorTintColor = UIColor.GroupTableViewBackgroundColor;
			temp.Pages = 4; 
			temp.CurrentPage = 0;
			//temp.Layer.BorderColor = UIColor.Black.CGColor;
			//temp.Layer.BorderWidth = 1f;
			AddSubview (temp);
		}
	}
}

